#pragma once

#include <AVUIUIElement.h>
#include <AVUIObjectCollection.h>

namespace AVUI {

class UIElementCollection : public TObjectCollection<UIElement>
{
    friend class Panel;
public:
    DECLARE_ELEMENT(UIElementCollection, ObjectCollectionBase);
    UIElementCollection() : m_pOwner(NULL), m_isNotLogical(false), m_isDestroying(false) { }
    UIElementCollection(FrameworkElement* pOwner) : m_pOwner(pOwner), m_isNotLogical(false), m_isDestroying(false) { }
    ~UIElementCollection() { m_isDestroying = true; Clear(); }

protected:

	virtual void ClearItems()
	{
		for(int idx = 0; idx < get_Count(); idx++)
		{
			OnRemove((*this)[idx]);
		}

		TObjectCollection<UIElement>::ClearItems();
	}

	virtual void InsertItem(int index, UIElement* pItem)
	{
		TObjectCollection<UIElement>::InsertItem(index, pItem);

		OnAdd(pItem);
	}

	virtual void RemoveItem(int index)
	{
		TRefCountedPtr<UIElement> pOldValue = (*this)[index];
		TObjectCollection<UIElement>::RemoveItem(index);

		OnRemove(pOldValue);
	}

	virtual void SetItem(int index, UIElement* pItem)
	{
		TRefCountedPtr<UIElement> pOldValue = (*this)[index];
		TObjectCollection<UIElement>::SetItem(index, pItem);

		OnRemove(pOldValue);
		OnAdd(pItem);
	}

private:

	void OnAdd(UIElement* pAdd);
	void OnRemove(UIElement* pRemove);

    void set_IsNotLogical(bool isNotLogical) { m_isNotLogical = isNotLogical; }
    bool get_IsNotLogical() { return m_isNotLogical; }


    FrameworkElement* m_pOwner;
    bool m_isNotLogical;
    bool m_isDestroying;
};


}; // namespace AVUI
